//
//  UIImage+Extensions.swift
//  myhub
//
//  Created by x on 2024/5/14.
//

import UIKit

extension UIImage: DesignWarapper {}
    
extension Wrapper where Base: UIImage {
    
    public func filled(withColor color: UIColor) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(base.size, false, base.scale)
        color.setFill()
        
        guard let context = UIGraphicsGetCurrentContext() else { return base }
        context.translateBy(x: 0, y: base.size.height)
        context.scaleBy(x: 1.0, y: -1.0)
        context.setBlendMode(CGBlendMode.normal)
        
        let rect = CGRect(x: 0, y: 0, width: base.size.width, height: base.size.height)
        
        guard let mask = base.cgImage else { return base }
        context.clip(to: rect, mask: mask)
        context.fill(rect)
        
        let newImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        return newImage
    }
}
